package cloud.estate.chargeItem.service.impl;

import cloud.estate.chargeItem.service.ChargeItemService;
import cloud.estate.commons.bean.ChargeItem;
import cloud.estate.commons.handler.exception.*;
import cloud.estate.commons.mapper.ChargeItemMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChargeItemServiceImpl extends ServiceImpl<ChargeItemMapper, ChargeItem> implements ChargeItemService {
    @Override
    public JsonResult addOneChargeItem(ChargeItem chargeItem){
        try {
            boolean save = save(chargeItem);
            if (save){
                return ResultTool.success("添加成功");
            }
            return ResultTool.fail(501,"添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult deleteOneChargeItem(String id){
        try {
            boolean removeById = removeById(id);
            if (removeById){
                return ResultTool.success("删除成功");
            }
            return ResultTool.fail(502,"删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }
    @Override
    public JsonResult updateOneChargeItem(ChargeItem chargeItem){
        try {
            boolean updateById = updateById(chargeItem);
            if (updateById){
                return ResultTool.success("修改成功");
            }
            return ResultTool.fail(503,"修改失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult findOneChargeItem(String id){
        try {
            ChargeItem chargeItem = getById(id);
            if (chargeItem != null){
                return ResultTool.success(chargeItem);
            }
            return ResultTool.fail(504,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult findAllChargeItem(){
        try {
            List<ChargeItem> list = list();
            if (list != null){
                return ResultTool.success(list);
            }
            return ResultTool.fail(504,"查询失败");
        } catch (Exception e) {
            throw new FindAllException(e.getMessage());
        }
    }
}
